package ar.com.ford.it.fullEconomics.view.form;

import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import org.displaytag.exception.DecoratorException;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp04Supply;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp05Pricing;
import ar.com.ford.it.fullEconomics.persistence.dto.Teess01Supplier;
import ar.com.ford.it.fullEconomics.struct.PricingStruct;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;
import ar.com.ford.it.fullEconomics.view.displaytag.DateDecorator;

public class PricingForm extends ValidatorForm {

    String selectedRowNum = null;
    String modePanel = "";
    String operation = "";
    String supplierF = null;
    String partF = null;
    String gsdbF = null;
    String partCF = null;
    String partPreCF = null;
    String partBaseF = null;
    String partSufixF = null;
    String dateInEffStart = null;
    String dateInEffEnd = null;
    String dateOutEffStart = null;
    String dateOutEffEnd = null;
    Teesp04Supply supplyNew = new Teesp04Supply();
    Teesp01Part teesp01Part = new Teesp01Part();
    Teess01Supplier teess01Supplier = new Teess01Supplier();
    Teesp05Pricing selcetedPricing = new Teesp05Pricing();
    Collection<PricingStruct> pricingS = null;
    String currencyNew = null;
    String dateInEffStartNew = null;
    String dateOutEffEndNew = null;
    String priceNew = null;
    String supplierCodeF = null;

    public String getSupplierCodeF() {
        return supplierCodeF;
    }

    public void setSupplierCodeF(String supplierCodeF) {
        this.supplierCodeF = supplierCodeF;
    }

    public String getPriceNew() {
        return priceNew;
    }

    public void setPriceNew(String priceNew) {
        this.priceNew = priceNew;
    }

    public Collection<PricingStruct> getPricingS() {
        return pricingS;
    }

    public void setPricingS(Collection<PricingStruct> pricingS) {
        this.pricingS = pricingS;
    }

    public void resetPanelFind() {
        this.supplierF = "-1";
        this.partF = "";
        this.gsdbF = "";
        this.partCF = "-1";
        this.partPreCF = "";
        this.partBaseF = "";
        this.partSufixF = "";
        this.dateInEffStart = "";
        this.dateInEffEnd = "";
        this.dateOutEffStart = "";
        this.dateOutEffEnd = "";
        this.supplierCodeF = "";

    }

    public void resetPanelCreateUpdate() {
        this.teesp01Part = new Teesp01Part();
        this.teess01Supplier = new Teess01Supplier();
        this.priceNew = "";
        this.currencyNew = "";
        this.dateOutEffEndNew = null;
        this.dateInEffStartNew = null;

    }

    public void setClearParametersPanel() {
        setTeesp01Part(new Teesp01Part());
        setTeess01Supplier(new Teess01Supplier());
        setCurrencyNew("");
        setPriceNew("");
    }

    public void resetForm() {
        this.supplierF = null;
        this.partF = null;
        this.gsdbF = null;
        this.partCF = null;
        this.partPreCF = null;
        this.partBaseF = null;
        this.partSufixF = null;
        this.dateInEffStart = null;
        this.dateInEffEnd = null;
        this.dateOutEffStart = null;
        this.dateOutEffEnd = null;
        this.teesp01Part = new Teesp01Part();
        this.teess01Supplier = new Teess01Supplier();
        this.currencyNew = null;
        this.dateOutEffEndNew = null;
        this.dateInEffStartNew = null;
        this.operation = "";

    }

    public void setPricingMapped(String key, String value) {
        List pricingSS = (List) getPricingS();
        PricingStruct ecoS = (PricingStruct) pricingSS.get(Integer
                .parseInt(key) - 1);
        this.setSelcetedPricing(ecoS.getPricing());
    }

    public void setEditParametersPanel() {
        this.setTeesp01Part(this.getSelcetedPricing().getTeesp04Supply()
                .getTeesp01Part());
        this.setTeess01Supplier(this.getSelcetedPricing().getTeesp04Supply()
                .getTeess01Supplier());
        this.setCurrencyNew(this.getSelcetedPricing().getEesp05CurrencyCodeC()
                .getEesm01CurrencyCodeC());
        this.setPriceNew(this.getSelcetedPricing().getEesp05PriceAmountA()
                .toString());

        try {
            this.setDateInEffStartNew((new DateDecorator()).decorate(
                    this.getSelcetedPricing().getEesp05PriceEffectiveDateY(),
                    null, null).toString());
            this.setDateOutEffEndNew((new DateDecorator()).decorate(
                    this.getSelcetedPricing().getEesp05PriceEndDateY(), null,
                    null).toString());
        } catch (DecoratorException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public String getCurrencyNew() {
        return currencyNew;
    }

    public void setCurrencyNew(String currencyNew) {
        this.currencyNew = currencyNew;
    }

    public String getDateInEffEnd() {
        return dateInEffEnd;
    }

    public void setDateInEffEnd(String dateInEffEnd) {
        this.dateInEffEnd = dateInEffEnd;
    }

    public String getDateInEffStart() {
        return dateInEffStart;
    }

    public void setDateInEffStart(String dateInEffStart) {
        this.dateInEffStart = dateInEffStart;
    }

    public String getDateInEffStartNew() {
        return dateInEffStartNew;
    }

    public void setDateInEffStartNew(String dateInEffStartNew) {
        this.dateInEffStartNew = dateInEffStartNew;
    }

    public String getDateOutEffEnd() {
        return dateOutEffEnd;
    }

    public void setDateOutEffEnd(String dateOutEffEnd) {
        this.dateOutEffEnd = dateOutEffEnd;
    }

    public String getDateOutEffEndNew() {
        return dateOutEffEndNew;
    }

    public void setDateOutEffEndNew(String dateOutEffEndNew) {
        this.dateOutEffEndNew = dateOutEffEndNew;
    }

    public String getDateOutEffStart() {
        return dateOutEffStart;
    }

    public void setDateOutEffStart(String dateOutEffStart) {
        this.dateOutEffStart = dateOutEffStart;
    }

    public String getGsdbF() {
        return gsdbF;
    }

    public void setGsdbF(String gsdbF) {
        this.gsdbF = gsdbF;
    }

    public String getModePanel() {
        return modePanel;
    }

    public void setModePanel(String modePanel) {
        this.modePanel = modePanel;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public String getPartBaseF() {
        return partBaseF;
    }

    public void setPartBaseF(String partBaseF) {
        this.partBaseF = partBaseF;
    }

    public String getPartCF() {
        return partCF;
    }

    public void setPartCF(String partCF) {
        this.partCF = partCF;
    }

    public String getPartF() {
        return partF;
    }

    public void setPartF(String partF) {
        this.partF = partF;
    }

    public String getPartPreCF() {
        return partPreCF;
    }

    public void setPartPreCF(String partPreCF) {
        this.partPreCF = partPreCF;
    }

    public String getPartSufixF() {
        return partSufixF;
    }

    public void setPartSufixF(String partSufixF) {
        this.partSufixF = partSufixF;
    }

    public String getSelectedRowNum() {
        return selectedRowNum;
    }

    public void setSelectedRowNum(String selectedRowNum) {
        this.selectedRowNum = selectedRowNum;
    }

    public String getSupplierF() {
        return supplierF;
    }

    public void setSupplierF(String supplierF) {
        this.supplierF = supplierF;
    }

    public Teesp04Supply getSupplyNew() {
        return supplyNew;
    }

    public void setSupplyNew(Teesp04Supply supplyNew) {
        this.supplyNew = supplyNew;
    }

    public Teesp01Part getTeesp01Part() {
        return teesp01Part;
    }

    public void setTeesp01Part(Teesp01Part teesp01Part) {
        this.teesp01Part = teesp01Part;
    }

    public Teess01Supplier getTeess01Supplier() {
        return teess01Supplier;
    }

    public void setTeess01Supplier(Teess01Supplier teess01Supplier) {
        this.teess01Supplier = teess01Supplier;
    }

    /**
     * @return the selcetedPricing
     */
    public Teesp05Pricing getSelcetedPricing() {
        return selcetedPricing;
    }

    /**
     * @param selcetedPricing
     * the selcetedPricing to set
     */
    public void setSelcetedPricing(Teesp05Pricing selcetedPricing) {
        this.selcetedPricing = selcetedPricing;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        if (getModePanel().equals("edit")
                && getOperation().equals("createUpdate")) {
            try {
                if (Double.valueOf(getPriceNew()) < 0)
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "pricing.error.price"));
            } catch (Exception e) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "pricing.error.price"));
            }
            if (getCurrencyNew().equals("-1")) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "pricing.error.moneda"));
            }

            if ((FullEconomicsDateUtil.format(getDateInEffStartNew())) == null) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "pricing.error.fechaInicio"));
            }
            if (!StringUtils.isBlank(this.getDateOutEffEndNew())) {
                if (FullEconomicsDateUtil.getDifference(this
                        .getDateInEffStartNew(), this.getDateOutEffEndNew()) <= 0) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.date.range",
                                            resources
                                                    .getMessage("currencyValues.label.tableHeader.fechaOut"),
                                            resources
                                                    .getMessage("currencyValues.label.tableHeader.fechaIn")));
                }
            }

        }
        if (getModePanel().equals("new")
                && getOperation().equals("createUpdate")) {
            if (!getDateInEffStartNew().equals("")) {
                try {
                    if (Double.valueOf(getPriceNew()) < 0)
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("pricing.error.price"));
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "pricing.error.price"));
                }
            } else {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "pricing.error.noPrice"));
            }

            if (getCurrencyNew().equals("-1")) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "pricing.error.moneda"));
            }

            if (!getDateInEffStartNew().equals("")) {
                if ((FullEconomicsDateUtil.format(getDateInEffStartNew())) == null) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "pricing.error.fechaInicio"));
                }
                if (!StringUtils.isBlank(this.getDateOutEffEndNew())) {
                    if (FullEconomicsDateUtil
                            .getDifference(this.getDateInEffStartNew(), this
                                    .getDateOutEffEndNew()) <= 0) {
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "errors.date.range",
                                                resources
                                                        .getMessage("currencyValues.label.tableHeader.fechaOut"),
                                                resources
                                                        .getMessage("currencyValues.label.tableHeader.fechaIn")));
                    }
                }
            } else {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "pricing.error.fechaInicioNo"));
            }

            if (getTeess01Supplier().getEess01SupplierClassCodeC().equals("-1")
                    && getTeess01Supplier().getEess01SupplierCodeC().equals("")) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "pricing.error.noSupplier"));
            }
            if (getTeesp01Part().getEesp01PartClassCodeC().equals("-1")
                    && getTeesp01Part().getEesp01PartPrefixCodeC().equals("")
                    && getTeesp01Part().getEesp01PartBaseCodeC().equals("")
                    && getTeesp01Part().getEesp01PartSuffixCodeC().equals("")) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "pricing.error.noPart"));
            }

        }
        if ("find".equalsIgnoreCase(this.getOperation())) {
            if (!StringUtils.isBlank(this.getDateInEffStart())) {
                if (StringUtils.isBlank(this.getDateInEffEnd())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaIn")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateInEffStart()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateInEffStart()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateInEffStart()));
                }
            }
            if (!StringUtils.isBlank(this.getDateInEffEnd())) {
                if (StringUtils.isBlank(this.getDateInEffStart())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaIn")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateInEffEnd()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateInEffEnd()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateInEffEnd()));
                }
            }
            if (!StringUtils.isBlank(this.getDateOutEffStart())) {
                if (StringUtils.isBlank(this.getDateOutEffEnd())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaOut")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateOutEffStart()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateOutEffStart()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateOutEffStart()));
                }
            }
            if (!StringUtils.isBlank(this.getDateOutEffEnd())) {
                if (StringUtils.isBlank(this.getDateOutEffStart())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaOut")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateOutEffEnd()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateOutEffEnd()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateOutEffEnd()));
                }
            }
            if ((!getDateInEffStart().equals("") && FullEconomicsDateUtil
                    .format(this.getDateInEffStart()) != null)
                    && (!getDateInEffEnd().equals("") && FullEconomicsDateUtil
                            .format(this.getDateInEffEnd()) != null)) {
                if (FullEconomicsDateUtil.getDifference(this
                        .getDateInEffStart(), this.getDateInEffEnd()) <= 0) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "pricing.error.fechaEffectivaStart"));
                }
            }

            if ((!getDateOutEffStart().equals("") && FullEconomicsDateUtil
                    .format(this.getDateOutEffStart()) != null)
                    && (!getDateOutEffEnd().equals("") && FullEconomicsDateUtil
                            .format(this.getDateOutEffEnd()) != null)) {
                if (FullEconomicsDateUtil.getDifference(this
                        .getDateOutEffStart(), this.getDateOutEffEnd()) <= 0) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "pricing.error.fechaEffectivaOut"));
                }
            }

        }
        return ae;
    }
}
